package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.UserMainMenuPresenter;

public class UserMainMenuView extends Activity implements View.OnClickListener {
	private String PACKAGE_NAME;
	private UserMainMenuPresenter myPresenter;
	private Button reportLostItem, reportFoundItem, ViewRegisteredItems,
			BrowseItems, SearchForItems, viewOldest;
	private TextView AccountInfo, Logout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_main_menu);
		initialize();
		PACKAGE_NAME = getApplication().getPackageName();
		myPresenter = new UserMainMenuPresenter(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_display_main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onClick(View view) {
		Intent intent;
		switch (view.getId()) {
		case R.id.lostItem_button:
			intent = new Intent(this, ReportLostItemView.class);
			intent.setPackage(PACKAGE_NAME);
			myPresenter.reportLostItem(intent);
			break;
		case R.id.FoundItem_button:
			intent = new Intent(this, ReportFoundItemView.class);
			intent.setPackage(PACKAGE_NAME);
			myPresenter.reportFoundItem(intent);
			break;
		case R.id.ViewItem_button:
			intent = new Intent(this, ReportedItemsView.class);
			intent.setPackage(PACKAGE_NAME);
			myPresenter.showReportedItems(intent);
		case R.id.MatchItem_button:
			intent = new Intent(this, MatchedItemView.class);
			intent.setPackage(PACKAGE_NAME);
			myPresenter.showMatchedItems(intent);
			break;
		case R.id.Logout:
			intent = new Intent(this, LoginView.class);
			intent.setPackage(PACKAGE_NAME);
			myPresenter.logout(intent);
			break;
		case R.id.Myaccount:
			intent = new Intent(this, AccountInformationView.class);
			intent.setPackage(PACKAGE_NAME);
			myPresenter.showAccountInfo(intent);
			break;
		case R.id.searchButton:
			intent = new Intent(this, ShowSearchView.class);
			intent.setPackage(PACKAGE_NAME);
			myPresenter.showSearchPage(intent);
			break;
		case R.id.ViewOldestItem:
			intent = new Intent(this, ShowItemView.class);
			intent.setPackage(PACKAGE_NAME);
			myPresenter.showOldestItem(intent);
		}
	}

	/**
	 * Initializes all of the views in the XML layout file, and sets the listeners for the buttons
	 */
	private void initialize() {
		reportLostItem = (Button) findViewById(R.id.lostItem_button);
		reportLostItem.setOnClickListener(this);

		reportFoundItem = (Button) findViewById(R.id.FoundItem_button);
		reportFoundItem.setOnClickListener(this);

		ViewRegisteredItems = (Button) findViewById(R.id.ViewItem_button);
		ViewRegisteredItems.setOnClickListener(this);

		BrowseItems = (Button) findViewById(R.id.MatchItem_button);
		BrowseItems.setOnClickListener(this);

		AccountInfo = (TextView) findViewById(R.id.Myaccount);
		AccountInfo.setOnClickListener(this);

		Logout = (TextView) findViewById(R.id.Logout);
		Logout.setOnClickListener(this);

		SearchForItems = (Button) findViewById(R.id.searchButton);
		SearchForItems.setOnClickListener(this);
		
		viewOldest = (Button) findViewById(R.id.ViewOldestItem);
		viewOldest.setOnClickListener(this);
	}

}
